/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swing.formularios;

import hibernate.entidades.Proveedores;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import org.hibernate.exception.DataException;
import swing.InterfazPrincipal;
import swing.modelos.ModeloProveedores;

/**
 *
 * @author ariel
 */
public class FormProveedores extends javax.swing.JDialog {

    ModeloProveedores modeloProveedores;
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("swing.formularios/Bundle");

    /**
     * Creates new form FormClientes
     */
    public FormProveedores(ModeloProveedores modeloProveedores, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.modeloProveedores = modeloProveedores;
        initComponents();
        getRootPane().setDefaultButton(botonNew);
    }

    public FormProveedores(ModeloProveedores modeloProveedores, InterfazPrincipal aThis, boolean b, int selectedRow) {
        this(modeloProveedores, aThis, b);
        Proveedores proveedor = modeloProveedores.getRow(selectedRow);
        this.campoCodigo.setEditable(false);
        this.campoCodigo.setText(proveedor.getCodProv());
        this.campoNombre.setText(proveedor.getNombre());
        this.campoEmail.setText(proveedor.getEmail());
        this.campoDireccion.setText(proveedor.getDireccion());
        this.campoTelefono.setText(proveedor.getTelf());
        this.campoFax.setText(proveedor.getFax());
        botonNew.setText(bundle.getString("textoBotonEditar"));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelPrincipal = new javax.swing.JPanel();
        panelFormulario = new javax.swing.JPanel();
        campoCodigo = new javax.swing.JTextField();
        etiquetaCodigoProv = new javax.swing.JLabel();
        etiquetaNombre = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        etiquetaDireccion = new javax.swing.JLabel();
        campoDireccion = new javax.swing.JTextField();
        etiquetaTelefono = new javax.swing.JLabel();
        campoTelefono = new javax.swing.JTextField();
        etiquetaEmail2 = new javax.swing.JLabel();
        campoEmail = new javax.swing.JTextField();
        etiquetaFax = new javax.swing.JLabel();
        campoFax = new javax.swing.JTextField();
        botonNew = new javax.swing.JButton();
        botonCanel = new javax.swing.JButton();
        botonDelete = new javax.swing.JButton();
        etiquetaTitulo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("swing/formularios/Bundle"); // NOI18N
        panelFormulario.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("FormProveedores.panelFormulario.border.title"))); // NOI18N

        etiquetaCodigoProv.setText(bundle.getString("FormProveedores.etiquetaCodigoProv.text")); // NOI18N

        etiquetaNombre.setText(bundle.getString("NOMBRE")); // NOI18N

        etiquetaDireccion.setText(bundle.getString("FormProveedores.etiquetaDireccion.text")); // NOI18N

        etiquetaTelefono.setText(bundle.getString("TELÉFONO")); // NOI18N

        etiquetaEmail2.setText(bundle.getString("FormProveedores.etiquetaEmail2.text")); // NOI18N

        etiquetaFax.setText(bundle.getString("FormProveedores.etiquetaFax.text")); // NOI18N

        javax.swing.GroupLayout panelFormularioLayout = new javax.swing.GroupLayout(panelFormulario);
        panelFormulario.setLayout(panelFormularioLayout);
        panelFormularioLayout.setHorizontalGroup(
            panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormularioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(etiquetaDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(etiquetaNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(etiquetaCodigoProv, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(etiquetaEmail2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelFormularioLayout.createSequentialGroup()
                        .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(etiquetaTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(etiquetaFax, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(campoFax, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(campoEmail)
                    .addComponent(campoDireccion)
                    .addComponent(campoNombre))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelFormularioLayout.setVerticalGroup(
            panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormularioLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(etiquetaCodigoProv)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(etiquetaNombre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(etiquetaDireccion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelFormularioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(panelFormularioLayout.createSequentialGroup()
                        .addComponent(etiquetaTelefono)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelFormularioLayout.createSequentialGroup()
                        .addComponent(etiquetaFax)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoFax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(etiquetaEmail2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
        );

        botonNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/add.png"))); // NOI18N
        botonNew.setText(bundle.getString("CREAR")); // NOI18N
        botonNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNewActionPerformed(evt);
            }
        });

        botonCanel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancel.png"))); // NOI18N
        botonCanel.setText(bundle.getString("CANCELAR")); // NOI18N
        botonCanel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCanelActionPerformed(evt);
            }
        });

        botonDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/gnome_edit_delete.png"))); // NOI18N
        botonDelete.setText(bundle.getString("BORRAR")); // NOI18N
        botonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDeleteActionPerformed(evt);
            }
        });

        etiquetaTitulo.setFont(new java.awt.Font("Trajan Pro", 0, 24)); // NOI18N
        etiquetaTitulo.setForeground(new java.awt.Color(153, 51, 0));
        etiquetaTitulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaTitulo.setText(bundle.getString("NUEVO PROVEEDOR")); // NOI18N

        javax.swing.GroupLayout panelPrincipalLayout = new javax.swing.GroupLayout(panelPrincipal);
        panelPrincipal.setLayout(panelPrincipalLayout);
        panelPrincipalLayout.setHorizontalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPrincipalLayout.createSequentialGroup()
                .addGroup(panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(etiquetaTitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
                    .addComponent(panelFormulario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPrincipalLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonNew)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonCanel)
                .addContainerGap())
        );
        panelPrincipalLayout.setVerticalGroup(
            panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPrincipalLayout.createSequentialGroup()
                .addComponent(etiquetaTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelFormulario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(botonDelete)
                    .addComponent(botonCanel)
                    .addComponent(botonNew))
                .addGap(48, 48, 48))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-538)/2, (screenSize.height-476)/2, 538, 476);
    }// </editor-fold>//GEN-END:initComponents

    private void botonCanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCanelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_botonCanelActionPerformed

    private void botonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDeleteActionPerformed
        // TODO add your handling code here:
        Component[] components = panelFormulario.getComponents();
        for (int i = 0; i < components.length; i++) {
            if (components[i].getClass() == campoCodigo.getClass()) {
                ((JTextField) components[i]).setText("");
            }
        }
    }//GEN-LAST:event_botonDeleteActionPerformed

    private void botonNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNewActionPerformed
        try {
            // TODO add your handling code here:
            Proveedores proveedor;
            proveedor = new Proveedores(campoCodigo.getText());
            proveedor.setNombre(campoNombre.getText());
            proveedor.setEmail(campoEmail.getText());
            proveedor.setDireccion(campoDireccion.getText());
            proveedor.setTelf(campoTelefono.getText());
            proveedor.setFax(campoFax.getText());


            /**
             * obtener el owner modeloClientes.
             */
            this.modeloProveedores.addRow(proveedor);
            this.modeloProveedores.fireTableDataChanged();
            this.dispose();
        } catch (DataException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.OK_OPTION);

        }
    }//GEN-LAST:event_botonNewActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCanel;
    private javax.swing.JButton botonDelete;
    private javax.swing.JButton botonNew;
    private javax.swing.JTextField campoCodigo;
    private javax.swing.JTextField campoDireccion;
    private javax.swing.JTextField campoEmail;
    private javax.swing.JTextField campoFax;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoTelefono;
    private javax.swing.JLabel etiquetaCodigoProv;
    private javax.swing.JLabel etiquetaDireccion;
    private javax.swing.JLabel etiquetaEmail2;
    private javax.swing.JLabel etiquetaFax;
    private javax.swing.JLabel etiquetaNombre;
    private javax.swing.JLabel etiquetaTelefono;
    private javax.swing.JLabel etiquetaTitulo;
    private javax.swing.JPanel panelFormulario;
    private javax.swing.JPanel panelPrincipal;
    // End of variables declaration//GEN-END:variables
}
